﻿using System;
using UnityEngine;
using UnityEngine.Events;
using Valve.VR;

// Token: 0x02000825 RID: 2085
public class SteamVR_Fade : MonoBehaviour
{
	// Token: 0x0600300D RID: 12301 RVA: 0x000F7904 File Offset: 0x000F5B04
	public static void Start(Color newColor, float duration, bool fadeOverlay = false)
	{
		SteamVR_Events.Fade.Send(newColor, duration, fadeOverlay);
	}

	// Token: 0x0600300E RID: 12302 RVA: 0x000F7914 File Offset: 0x000F5B14
	public static void View(Color newColor, float duration)
	{
		CVRCompositor compositor = OpenVR.Compositor;
		if (compositor != null)
		{
			compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
		}
	}

	// Token: 0x0600300F RID: 12303 RVA: 0x000F7954 File Offset: 0x000F5B54
	public void OnStartFade(Color newColor, float duration, bool fadeOverlay)
	{
		if (duration > 0f)
		{
			this.targetColor = newColor;
			this.deltaColor = (this.targetColor - this.currentColor) / duration;
		}
		else
		{
			this.currentColor = newColor;
		}
	}

	// Token: 0x06003010 RID: 12304 RVA: 0x000F7994 File Offset: 0x000F5B94
	private void OnEnable()
	{
		if (SteamVR_Fade.fadeMaterial == null)
		{
			SteamVR_Fade.fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
			SteamVR_Fade.fadeMaterialColorID = Shader.PropertyToID("fadeColor");
		}
		SteamVR_Events.Fade.Listen(new UnityAction<Color, float, bool>(this.OnStartFade));
		SteamVR_Events.FadeReady.Send();
	}

	// Token: 0x06003011 RID: 12305 RVA: 0x000F79F4 File Offset: 0x000F5BF4
	private void OnDisable()
	{
		SteamVR_Events.Fade.Remove(new UnityAction<Color, float, bool>(this.OnStartFade));
	}

	// Token: 0x06003012 RID: 12306 RVA: 0x000F7A0C File Offset: 0x000F5C0C
	private void OnPostRender()
	{
		if (this.currentColor != this.targetColor)
		{
			if (Mathf.Abs(this.currentColor.a - this.targetColor.a) < Mathf.Abs(this.deltaColor.a) * Time.deltaTime)
			{
				this.currentColor = this.targetColor;
				this.deltaColor = new Color(0f, 0f, 0f, 0f);
			}
			else
			{
				this.currentColor += this.deltaColor * Time.deltaTime;
			}
			if (this.fadeOverlay)
			{
				SteamVR_Overlay instance = SteamVR_Overlay.instance;
				if (instance != null)
				{
					instance.alpha = 1f - this.currentColor.a;
				}
			}
		}
		if (this.currentColor.a > 0f && SteamVR_Fade.fadeMaterial)
		{
			SteamVR_Fade.fadeMaterial.SetColor(SteamVR_Fade.fadeMaterialColorID, this.currentColor);
			SteamVR_Fade.fadeMaterial.SetPass(0);
			GL.Begin(7);
			GL.Vertex3(-1f, -1f, 0f);
			GL.Vertex3(1f, -1f, 0f);
			GL.Vertex3(1f, 1f, 0f);
			GL.Vertex3(-1f, 1f, 0f);
			GL.End();
		}
	}

	// Token: 0x04002FE6 RID: 12262
	private Color currentColor = new Color(0f, 0f, 0f, 0f);

	// Token: 0x04002FE7 RID: 12263
	private Color targetColor = new Color(0f, 0f, 0f, 0f);

	// Token: 0x04002FE8 RID: 12264
	private Color deltaColor = new Color(0f, 0f, 0f, 0f);

	// Token: 0x04002FE9 RID: 12265
	private bool fadeOverlay;

	// Token: 0x04002FEA RID: 12266
	private static Material fadeMaterial;

	// Token: 0x04002FEB RID: 12267
	private static int fadeMaterialColorID = -1;
}
